var o Ormer
o = orm.NewOrm() //创建一个Ormer
// NewOrm 的同时会执行 orm.BootStrap（整个 app 只执行一次），用以验证模型之间的定义并缓存。



type Ormer interface {
    Read(interface{}, …string) error
    ReadOrCreate(interface{}, string, …string) (bool, int64, error)
    Insert(interface{}) (int64, error)
    InsertMulti(int, interface{}) (int64, error)
    Update(interface{}, …string) (int64, error)
    Delete(interface{}) (int64, error)
    LoadRelated(interface{}, string, …interface{}) (int64, error)
    QueryM2M(interface{}, string) QueryM2Mer
    QueryTable(interface{}) QuerySeter
    Using(string) error
    Begin() error
    Commit() error
    Rollback() error
    Raw(string, …interface{}) RawSeter
    Driver() Driver
}



o := orm.NewOrm()
user := models.User{Id: 1}
if err := o.Read(&user); err == nil {
    fmt.Println("查询成功, 用户名为:", user.Username, ", 密码为: ", user.Password)
} else {
    fmt.Println("查询失败")
}








o := orm.NewOrm()

var user models.User
// 获取 QuerySeter 对象，user 为表名
qs := o.QueryTable("user")

// 也可以直接使用对象作为表名
// user := new(User)
// qs = o.QueryTable(user) // 返回 QuerySeter
qs := qs.Filter("UserName", "Name").Filter("Password", "0000").One(&user)




<div class="form" style="text-align:center">
  <form action="/api/user/register" method="POST">
    用户名:<br />
    <input type="text" name="Username" />
    <br /><br />密码:<br />
    <input type="password" name="Password" />
    <br /><br /><button type="submit">提交</button>
  </form>
</div>




















package controllers

import (
    "github.com/astaxie/beego"
    "myproject/models" // 在这里要去掉下划线,因为接下来会使用 models 这个包
    "github.com/astaxie/beego/orm"  // 新加引用
    "fmt"                           // 新加引用
)

........省略内容........

// 获取用户信息
func (c *MainController) UserInfo() {
    defer c.ServeJSON()
    user_id, _ := c.GetInt(":user_id")
    user := models.User{Id: user_id}
    o := orm.NewOrm()
    o.Read(&user)

    c.Data["json"] = map[string]interface{}{"ok": true,
        "data": map[string]interface{}{"UserId": user_id, "username": user.Username, "passowrd": user.Password}}
}

//用户注册
func (this *MainController) UserRegister() {
    defer this.ServeJSON()
    username := this.GetString("Username")
    password := this.GetString("Password")
    o := orm.NewOrm()
    user := models.User{Username:username, Password:password}
    //判断该用户是否存在
    if o.QueryTable("user").Filter("Username", username).Exist() {
        this.Data["json"] = map[string]interface{}{"ok": false, "message": "Username: " + username + ", 该用户已经存在" }
        return
    } else {
        id, err := o.Insert(&user)
        if err != nil {
            //debug使用
            fmt.Println("err: ", err.Error())
            return
        } else {
            //debug使用
            fmt.Println("id: ", id)
        }
        this.Data["json"] = map[string]interface{}{
            "ok": true,
            "data": map[string]interface{}{
                "data":     "恭喜你注册成功",
                "username": "用户名为:" + username,
                "password": "账号密码为:" + password,
                "用户Id":  id,
            }}
    }

}







func (c *MainController) Get() {
    c.Data["Website"] = "beego.me"
    c.Data["Email"] = "astaxie@gmail.com"
    c.TplName = "index.tpl"
}




















